package play.cnblog.cli.service.impl;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import play.cnblog.cli.model.Blog;
import play.cnblog.cli.rpc.CnblogRpc;
import play.cnblog.cli.service.BlogService;
import play.cnblog.cli.service.CnblogMediaService;
import play.cnblog.cli.service.CnblogService;

/**
 * @author LiJie
 */
public class CnblogServiceImpl implements CnblogService {

    private final CnblogRpc cnblogRpc;
    private final BlogService blogService;
    private final CnblogMediaService cnblogMediaService;

    public CnblogServiceImpl(CnblogRpc cnblogRpc, BlogService blogService,
        CnblogMediaService cnblogMediaService) {
        this.cnblogRpc = cnblogRpc;
        this.blogService = blogService;
        this.cnblogMediaService = cnblogMediaService;
    }

    @Override
    public String add(String filePath) {
        Blog blog = blogService.parseFile(filePath);
        if (StringUtils.isNotBlank(blog.getPostid())) {
            throw new RuntimeException(
                "This blog already exists, please execute `update` command");
        }
        cnblogMediaService.uploadMedia(blog);
        String blogId = cnblogRpc.createBlog(blog);
        if (Objects.isNull(blogId)) {
            throw new RuntimeException("cnblog's blog create fail");
        }
        if (addId(blogId, filePath)) {
            return blogId;
        }
        else {
            throw new RuntimeException("blog id [" + blogId + "] add fail.");
        }
    }

    @Override
    public boolean update(String filePath) {
        Blog blog = blogService.parseFile(filePath);
        if (StringUtils.isBlank(blog.getPostid())) {
            throw new RuntimeException
                ("[" + filePath + "] blog's id not exist.");
        }
        cnblogMediaService.uploadMedia(blog);
        return cnblogRpc.updateBlog(blog.getPostid(), blog);
    }

    @Override
    public boolean delete(String filePath) {
        String blogId = getId(filePath);
        if (StringUtils.isBlank(blogId)) {
            throw new RuntimeException("blog's id not exist.");
        }
        return cnblogRpc.deleteBlog(blogId);
    }

    @Override
    public String uploadMedia(String filePath) {
        return null;
    }

    @Override
    public Blog get(String filePath) {
        String blogId = getId(filePath);
        if (StringUtils.isBlank(blogId)) {
            throw new RuntimeException("blog's id not exist.");
        }
        return cnblogRpc.getBlog(blogId);
    }

    private String getId(String filePath) {
        List<String> contents;
        try {
            contents = Files.readAllLines(getPath(filePath));
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
        String firstLine = contents.get(0);
        if (Objects.isNull(firstLine)) {
            throw new RuntimeException("blog's id not exist.");
        }
        return firstLine.split("\\|")[0];
    }

    private boolean addId(String id, String filePath) {
        List<String> contents;
        try {
            contents = Files.readAllLines(getPath(filePath));
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
        String firstLine = contents.get(0);
        contents.set(0, id + firstLine);
        try {
            Files.write(getPath(filePath), contents);
            return true;
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private Path getPath(String filePath) {
        return Paths.get(filePath);
    }
}
